package abservlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.StudentInfo;
import bean.page;

public class ALLServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	// 
	public Connection Connect() throws ClassNotFoundException, SQLException {
		Connection conn = null;
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://localhost/yyy";
		String user = "root";
		String password = "root";
		conn = DriverManager.getConnection(url, user, password);
		return conn;
	}

	public void close(Statement stat, Connection conn) throws SQLException,
			IOException {
		if (stat != null) {
			stat.close();
		}
		if (conn != null) {
			conn.close();
		}
	}

	public void insert(HttpServletRequest request, HttpServletResponse response)
			throws ClassNotFoundException, SQLException, IOException {
		Connection conn = null;
		Statement stat = null;
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		String gender = request.getParameter("gender");
		String major = request.getParameter("major");
		conn = Connect();
		stat = conn.createStatement();
		stat.execute("insert into student(id,name,age,gender,major) values("
				+ id + "," + name + "," + age + "," + gender + "," + major
				+ ")");
		close(stat, conn);
		// SELECT * FROM student WHERE id = " + id;
		// id -> 1 = 1
		// preparedStatment.
	}

	public ArrayList<StudentInfo> Select(String id, String name)
			throws ClassNotFoundException, SQLException, IOException {
		Connection conn = null;
		Statement stat = null;
		ResultSet rs = null;
		conn = Connect();
		stat = conn.createStatement();
		ArrayList<StudentInfo> result = new ArrayList<StudentInfo>();
		if (id == "" && name == "") {
			rs = stat.executeQuery("select*From student");
		}
		if (id == "" && name == "") {
			rs = stat.executeQuery("select*From student where id=" + id + "");
		}
		if (id == "&&name!=") {
			rs = stat.executeQuery("select*From student where name=" + name
					+ "");
		}
		if (id == "" && name != "") {
			rs = stat.executeQuery("select*From student where id=" + id
					+ "and name=" + name + "");
		}
		while (rs.next()) {
			StudentInfo st = new StudentInfo();
			st.setId(rs.getInt("id"));
			st.setName(rs.getString("name"));
			st.setAge(rs.getInt("age"));
			st.setGender(rs.getString("gender"));
			st.setMajor(rs.getString("major"));
			result.add(st);
		}
		if (rs != null) {
			rs.close();
		}
		close(stat, conn);
		return result;
	}

	public void dispatch(HttpServletRequest request,
			HttpServletResponse response) throws ClassNotFoundException,
			SQLException, ServletException, IOException {
		String id5 = request.getParameter("id");
		String name5 = request.getParameter("name");
		if (Select(id5, name5).isEmpty()) {
			request.getRequestDispatcher("selectnothing.jsp").forward(request,
					response);
		} else {
			request.setAttribute("result", Select(id5, name5));
			request.getRequestDispatcher("idnameselect.jsp").forward(request,
					response);
		}

	}
	public page setpage(HttpServletRequest request,
			HttpServletResponse response) throws ClassNotFoundException,
			SQLException, ServletException, IOException {
		String crd = request.getParameter("currentRecord");
		 ArrayList<StudentInfo>result=Select("", "");
		 page pager=new page();
		 pager.setTotalRecord(result.size());
		 pager.setTotallPage(result.size(),pager.getPageSize());
		if (crd!=null) {
			int currentRecord=Integer.parseInt(crd);
			pager.setCurrentRecord(currentRecord);
			pager.setCurrentPage(currentRecord,pager.getPageSize());
		}
		return pager;
	}

	public void difpage(HttpServletRequest request,
			HttpServletResponse response) throws ClassNotFoundException,
			SQLException, ServletException, IOException {
		 ArrayList<StudentInfo>result=Select("", "");
		 page pager=new page();
		 pager=setpage(request, response);
		 List<StudentInfo>subResult=null;
		 int currentRecord=pager.getCurrentRecord();
		 if (currentRecord==0){
			 if(pager.getTotalRecord()<8){
				 subResult=(List<StudentInfo>)result.subList(0,pager.getTotalRecord());{
					 subResult=(List<StudentInfo>)result.subList(pager.getTotalRecord(),pager.getCurrentRecord()+pager.getPageSize());
				 }
			 }
				 else {
					subResult=(List<StudentInfo>)result.subList(pager.getCurrentRecord(), result.size());
				}
			 request.setAttribute("pager", pager);
			 request.setAttribute("subResult",subResult);
			 request.getRequestDispatcher("layout.jsp").forward(request, response);
			 }
	}	 
		 
			 

	public void delete(HttpServletRequest request, HttpServletResponse response)
			throws ClassNotFoundException, SQLException, ServletException,
			IOException {
		Connection conn = null;
		Statement stat = null;
		conn = Connect();
		stat = conn.createStatement();
		String id2 = request.getParameter("id");
		stat.execute("delete from student where id=" + id2 + "");
		request.getRequestDispatcher("delete.jsp").forward(request, response);
	}

	public void update1(HttpServletRequest request, HttpServletResponse response)
			throws ClassNotFoundException, SQLException, ServletException,
			IOException {
		Connection conn = null;
		Statement stat = null;
		String id3 = request.getParameter("id");
		String name3 = request.getParameter("name");
		String age3 = request.getParameter("age");
		String gender3 = request.getParameter("gender");
		String major3 = request.getParameter("major");
		conn = Connect();
		stat = conn.createStatement();
		stat.execute("update student set id=" + id3 + ",name='" + name3
				+ "',age=" + age3 + ",gender='" + gender3 + "',major='"
				+ major3 + "' where id=" + id3 + "");
		request.setAttribute("result", Select(id3, ""));
		request.getRequestDispatcher("update1.jsp").forward(request, response);
	}

	public void update(HttpServletRequest request, HttpServletResponse response)
			throws ClassNotFoundException, SQLException, ServletException,
			IOException {
		String id4 = request.getParameter("id");
		request.setAttribute("result", Select(id4, ""));
		request.getRequestDispatcher("update1.jsp").forward(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String methodName = request.getParameter("methodName");
		int method = Integer.parseInt(methodName);
		try {
			switch (method) {
			case 0:
				insert(request, response);
			case 1:
				difpage(request, response);
				break;
			case 2:
				delete(request, response);
				break;
			case 3:
				update1(request, response);
				break;
			case 4:
				update(request, response);
				break;
			case 5:
				dispatch(request, response);
				break;
			}
		 
		} catch (ClassNotFoundException e) {
		 
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
// Spring -> SpringMVC -> video -> 22 MyBatis -> AJAX -> 
// AJAX -> JSP view -> logic Servlet